//
//  YPInvestDetailSectionOneView.m
//  ChengHuiTong
//
//  Created by KiVen on 15/11/3.
//  Copyright © 2015年 张文权. All rights reserved.
//

#import "YPInvestDetailSectionOneView.h"
#import "YPBorrowTypeView.h"

@interface YPInvestDetailSectionOneView (){
    
    YPBorrowTypeView *_borrowTypeView;
}

@end

@implementation YPInvestDetailSectionOneView{
    
    NSArray * picArray;
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        

        //图标集
        _borrowTypeView = [[YPBorrowTypeView alloc]initWithFrame:CGRectMake(15, 10 , 0, 20)];
        [self addSubview:_borrowTypeView];
        
        UIImage *aawardImage = [UIImage imageNamed:@"jiangli"];
        _awardImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 14, aawardImage.size.width, aawardImage.size.height)];
        _awardImageView.image = aawardImage;
        [self addSubview:_awardImageView];
        
        _awardLabel = [[UILabel alloc]initWithFrame:CGRectMake(17, 0, 40, 16)];
        _awardLabel.font = [UIFont systemFontOfSize:10];
        _awardLabel.textAlignment = NSTextAlignmentLeft;
        _awardLabel.text = @"0.25%";
        _awardLabel.textColor = [UIColor whiteColor];
//        _awardLabel.backgroundColor = [UIColor yellowColor];
        [_awardImageView addSubview:_awardLabel];
        

        UIImageView * dashLineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH-20, 1)];
        dashLineImageView.image = [UIImage imageNamed:@"dash_line"];
        [self addSubview:dashLineImageView];
        
        UIImageView *itemNumberImageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-115, 10, 115, 18)];
        itemNumberImageView.image = [UIImage imageNamed:@"itemNumber"];
//        itemNumberImageView.backgroundColor = kColor_AppNavColor;
        [self addSubview:itemNumberImageView];
        //        项目编号
        _itemNumberLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 0, 115-10, 18)];
        _itemNumberLabel.backgroundColor = kColor_AppNavColor;
        _itemNumberLabel.textColor = [UIColor whiteColor];
        _itemNumberLabel.layer.cornerRadius = 5;
        _itemNumberLabel.clipsToBounds = YES;
        _itemNumberLabel.font = [UIFont systemFontOfSize:10];
        [itemNumberImageView addSubview:_itemNumberLabel];
        
        //       年华收益率
        _theYieldContentLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, SCREEN_WIDTH/2, 20)];
        _theYieldContentLabel.text = @"19.6%";
        _theYieldContentLabel.textColor = kColor_AppNavColor;
        _theYieldContentLabel.font = [UIFont systemFontOfSize:12];
        _theYieldContentLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_theYieldContentLabel];
        

        _timeLimitLabel = [[UILabel alloc]initWithFrame:CGRectMake( SCREEN_WIDTH/2, 50,  SCREEN_WIDTH/2, 20)];
        _timeLimitLabel.text = @"6个月";
        _timeLimitLabel.textColor = kColor_AppNavColor;
        _timeLimitLabel.font = [UIFont systemFontOfSize:21];
        _timeLimitLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_timeLimitLabel];

        NSArray *theYieldTitleArray = @[@"参考年化收益率",@"借款期限"];
        NSArray *balanceTitleArray = @[@"标的总额",@"还款方式"];

        for (int i = 0; i < 2; i++) {
            
            UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(i*(SCREEN_WIDTH/2), CGRectGetMaxY(_theYieldContentLabel.frame)+5, SCREEN_WIDTH/2, 15)];
            titleLabel.text = theYieldTitleArray[i];
            titleLabel.textColor = kColor_greyAshTitleColor;
            titleLabel.font = [UIFont systemFontOfSize:13];
            titleLabel.textAlignment = NSTextAlignmentCenter;
            [self addSubview:titleLabel];
            
            
            int horizontalLineLabelWith =(SCREEN_WIDTH-10-10-20)/2;
//            横线
            UILabel * horizontalLineLabel = [[UILabel alloc]initWithFrame:CGRectMake(10+i*(horizontalLineLabelWith+20), CGRectGetMaxY(titleLabel.frame)+20, horizontalLineLabelWith, .5)];
            horizontalLineLabel.backgroundColor = kColor_AppNavColor;
            [self addSubview:horizontalLineLabel];
            
            
            int start = CGRectGetMidY(_theYieldContentLabel.frame)+5;
            
            UILabel * verticalLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2,start+i*(40+20), .5, 40)];
            verticalLabel.backgroundColor = kColor_AppNavColor;
            [self addSubview:verticalLabel];
            
//            剩余可投   还款方式
            UILabel * balanceLabel = [[UILabel alloc]initWithFrame:CGRectMake(i*(SCREEN_WIDTH/2),CGRectGetMaxY(horizontalLineLabel.frame)+10, SCREEN_WIDTH/2, 15)];
            balanceLabel.text =balanceTitleArray[i];
            balanceLabel.textColor = kColor_greyAshTitleColor;
            balanceLabel.font = [UIFont systemFontOfSize:13];
            balanceLabel.textAlignment = NSTextAlignmentCenter;
            [self addSubview:balanceLabel];
            
        
            UILabel * contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(i*(SCREEN_WIDTH/2), CGRectGetMaxY(balanceLabel.frame)+10, SCREEN_WIDTH/2, 15)];
            contentLabel.tag = i+100;
            contentLabel.font = [UIFont systemFontOfSize:14];
            contentLabel.textAlignment = NSTextAlignmentCenter;
            [self addSubview:contentLabel];
            
        }
        
        
    }
    return self;
}

- (void)receiveInvestDetailModel:(YPInvestDetailModel *)detailModel{
    
    self.investDetailModel = detailModel;
    
    NSMutableArray *borrowType = [NSMutableArray arrayWithArray:detailModel.borrow_type];
    if ([borrowType containsObject:@5]) {
        [borrowType insertObject:@6 atIndex:[borrowType indexOfObject:@5]];
    }
    if ([borrowType containsObject:@10]) {
        NSInteger index = [borrowType indexOfObject:@10];
        [borrowType replaceObjectAtIndex:index withObject:[NSNumber numberWithInteger:7]];
    }
    
    _itemNumberLabel.text = [NSString stringWithFormat:@"项目编号:%@",self.investDetailModel.number];
    
    
    _theYieldContentLabel.attributedText = [AppTools getAttributedStringWithRate:detailModel.apr
                                                                         rateAdd:detailModel.apr_add
                                                                        fontSize:21];
    
    [_borrowTypeView setBorrowType:detailModel.borrow_type];
    
    if ([detailModel.award floatValue]==0) {
        
        _awardImageView.hidden = YES;
    }else{
        
        _awardImageView.hidden = NO;
        _awardLabel.text = [NSString stringWithFormat:@"%@%%",detailModel.award];
        _awardImageView.left = _borrowTypeView.right;
        
    }
    
    
    
//    剩余可投
    UILabel * remainingLabel = [self viewWithTag:100];
    remainingLabel.text = self.investDetailModel.account;
    
//    还款期限
    if ([self.investDetailModel.is_datetype intValue]==0) {
        
        
        
        _timeLimitLabel.text= [NSString stringWithFormat:@"%@个月",self.investDetailModel.time_limit];
        
        
        
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:_timeLimitLabel.text];
        
        
        NSRange strRange = {self.investDetailModel.time_limit.length,2};
        
        [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16]range:strRange];
        _timeLimitLabel.attributedText = str;


        
    }else if ([self.investDetailModel.is_datetype intValue]==1){
        
        _timeLimitLabel.text= [NSString stringWithFormat:@"%@天",self.investDetailModel.time_limit];
        
        
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:_timeLimitLabel.text];
        
        
        NSRange strRange = {self.investDetailModel.time_limit.length,1};
        
        [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16]range:strRange];
        
        _timeLimitLabel.attributedText = str;

        
    }
    
//    还款方式
    UILabel * reimbursementMeansLabel = [self viewWithTag:101];
    
    reimbursementMeansLabel.text = YPGetRepayments(self.investDetailModel.style);

}

@end
